package com.taxx.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.taxx.entity.SysUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 系统用户Mapper接口
 */
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {
    
    /**
     * 根据用户名查询用户
     */
    @Select("SELECT * FROM sys_user WHERE username = #{username} AND deleted = 0")
    SysUser findByUsername(@Param("username") String username);
    
    /**
     * 根据用户名查询用户（包含角色信息）
     */
    @Select("SELECT u.*, GROUP_CONCAT(r.role_code) as role_codes " +
            "FROM sys_user u " +
            "LEFT JOIN sys_user_role ur ON u.id = ur.user_id " +
            "LEFT JOIN sys_role r ON ur.role_id = r.id " +
            "WHERE u.username = #{username} AND u.deleted = 0 " +
            "GROUP BY u.id")
    SysUser findByUsernameWithRoles(@Param("username") String username);
    
    /**
     * 查询用户列表
     */
    @Select("SELECT u.*, GROUP_CONCAT(r.role_name) as role_names " +
            "FROM sys_user u " +
            "LEFT JOIN sys_user_role ur ON u.id = ur.user_id " +
            "LEFT JOIN sys_role r ON ur.role_id = r.id " +
            "WHERE u.deleted = 0 " +
            "GROUP BY u.id " +
            "ORDER BY u.create_time DESC")
    List<SysUser> selectUserList();
}

